package com.kaili.basic.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.MdmAlarmPageListDto;
import com.kaili.basic.pojo.dto.MdmAlarmSetPageListDto;
import com.kaili.basic.pojo.entity.MdmAlarmSetEntity;
import com.kaili.basic.pojo.entity.MdmAlarmSubEntity;
import com.kaili.basic.pojo.vo.MdmAlarmPageListVo;
import com.kaili.basic.pojo.vo.MdmAlarmSetPageListVo;
import com.kaili.basic.pojo.vo.MdmAlarmSetVo;
import com.kaili.basic.pojo.vo.MdmAlarmSubVo;
import com.kaili.basic.service.IMdmAlarmService;
import com.kaili.basic.service.IMdmAlarmSetService;
import com.kaili.basic.service.IMdmAlarmSubService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
 * @Desc:
 * @author: jianfeng
 * @Time: 2019/8/2 15:36
 * @Copyright: © 2018 杭州凯立通信有限公司 版权所有
 * @Warning: 本内容仅限于公司内部传阅, 禁止外泄或用于其它商业目的
 */
@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "告警管理",description = "告警管理相关API")
public class MdmAlarmController {

    @Autowired
    private IMdmAlarmService alarmService;

    @Autowired
    private IMdmAlarmSubService alarmSubService;

    @Autowired
    private IMdmAlarmSetService alarmSetService;

    @GetMapping("/alarm/queryPageList")
    @ApiOperation(value = "分页查询告警信息",notes = "分页查询告警信息")
    public Page<MdmAlarmPageListVo> queryPageList(@ModelAttribute MdmAlarmPageListDto alarmPageListDto){
        return alarmService.queryPageList(new Page<>(alarmPageListDto.getPage(),alarmPageListDto.getPageSize()),alarmPageListDto);
    }

    @GetMapping("/alarm/exportAlarm")
    @ApiOperation(value = "导出告警明细",notes = "导出")
    public void export(HttpServletResponse response,@ModelAttribute MdmAlarmPageListDto alarmPageListDto) throws RestException {
        List<MdmAlarmPageListVo> list=alarmService.export(alarmPageListDto);
        ExcelUtil.exportExcel(list,"告警明细","sheet1", MdmAlarmPageListVo.class, UUID.randomUUID().toString()+".xls",response);
    }

    @PutMapping("/alarm/updateBatchSub")
    @ApiOperation(value = "批量修改订阅",notes = "批量修改订阅")
    public RestStatus updateBatchSub(@RequestBody List<MdmAlarmSubEntity> entity){
        alarmSubService.updateBatch(entity);
        return CommonStatus.UPDATE_OK;
    }

    @GetMapping("/alarm/querySubList")
    @ApiOperation(value = "查询所有订阅",notes = "查询所有订阅")
    public List<MdmAlarmSubVo> querySubList(){
        return alarmSubService.querySubList();
    }


    @PutMapping("/alarm/updateBatchSet")
    @ApiOperation(value = "设置告警规则",notes = "设置告警规则")
    public RestStatus updateBatchSet(@RequestBody List<MdmAlarmSetEntity> entity){
        alarmSetService.updateBatch(entity);
        return CommonStatus.UPDATE_OK;
    }

    @GetMapping("/alarm/{id}")
    @ApiOperation(value = "根据id查询告警规则")
    public MdmAlarmSetVo queryById(@ApiParam("ID") @PathVariable("id") Integer id){
        return alarmSetService.queryById(id);
    }

    @GetMapping("/alarm/querySetList")
    @ApiOperation(value = "查询所有告警规则")
    public List<MdmAlarmSetVo> querySetList(){
        return alarmSetService.queryList();
    }

    @GetMapping("/alarm/querySetPageList")
    @ApiOperation(value = "分页查询告警项")
    public Page<MdmAlarmSetPageListVo> queryPageList(@ModelAttribute MdmAlarmSetPageListDto alarmSetPageListDto){
        return alarmSetService.queryPageList(new Page<>(alarmSetPageListDto.getPage(),alarmSetPageListDto.getPageSize()),alarmSetPageListDto);
    }

    @GetMapping("/alarm/exportSet")
    @ApiOperation(value = "导出告警列表")
    public void export(HttpServletResponse response, @ModelAttribute MdmAlarmSetPageListDto alarmSetPageListDto) throws RestException {
        List<MdmAlarmSetPageListVo> list= alarmSetService.export(alarmSetPageListDto);
        ExcelUtil.exportExcel(list,"告警列表","sheet1", MdmAlarmSetPageListVo.class, UUID.randomUUID().toString()+".xls",response);
    }

}
